﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlexiCommerce.Persistence.NH;
using FlexiCommerce.Components;
using NUnit.Framework;

namespace FlexiCommerce.Tests.Persistence.NH
{
    [TestFixture]
    public class LanguageRepositoryTests : DatabasePreparingBase
    {
        NHRepository<int, Language> repository;
        ISessionProvider sessionProvider;

        [SetUp]
        public override void SetUp()
        {
            base.SetUp();
            CreateDatabaseSchema();

            sessionProvider = CreateSessionProvider();
            repository = new NHRepository<int, Components.Language>(sessionProvider);
        }

        [Test]
        public void CanSave()
        {
            try
            {
 int itemID = SaveAnItem(Culture.English);
            Assert.AreNotEqual(0, itemID);

            using (repository)
            {
                Language item = repository.Get(itemID);
                Assert.AreEqual(item.ID, itemID);
                repository.Delete(item);
                repository.Flush();
            }
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }

        [Test]
        public void CanUpdate()
        {
            int itemID = SaveAnItem(Culture.English);

            using (repository)
            {
                Language item = repository.Get(itemID);
                Assert.AreEqual(Culture.English, item.Culture);
                item.Culture = Culture.Spanish;
                repository.SaveOrUpdate(item);
                repository.Flush();
            }

            using (repository)
            {
                Language item = repository.Get(itemID);
                Assert.AreEqual(Culture.Spanish,item.Culture);
                repository.Delete(item);
                repository.Flush();
            }
        }

        [Test]
        public void CanFindAll()
        {
            int item1ID = SaveAnItem(Culture.English);
            int item2ID = SaveAnItem(Culture.English);
            int item3ID = SaveAnItem(Culture.English);

            using (repository)
            {
                ICollection<Language> items = repository.FindAll();
                Assert.AreEqual(3, items.Count);
                repository.Flush();
            }
        }

        [Test]
        public void CanDelete()
        {
            int itemID = SaveAnItem(Culture.English);

            using (repository)
            {
                Language item = repository.Get(itemID);
                Assert.IsNotNull(item, "There should be a saved item.");
                repository.Delete(item);
                repository.Flush();
            }

            using (repository)
            {
                Language item = repository.Get(itemID);
                Assert.IsNull(item, "Item is supposed to be deleted");
                repository.Flush();
            }
        }

        [Test]
        public void CanFindGreaterThanID()
        {
            int item1ID = SaveAnItem(Culture.English);
            int item2ID = SaveAnItem(Culture.English);
            int item3ID = SaveAnItem(Culture.English);

            using (repository)
            {
                ICollection<Language> items = repository.FindAll(NHibernate.Criterion.Expression.Gt("ID", 1));
                Assert.AreEqual(2, items.Count);
                repository.Flush();
            }
        }



 


        private int SaveAnItem(Culture culture)
        {
            try
            {
                using (repository)
                {
                    Language item = CreateOneItem<Language>(0, "", null);
                    item.Culture = culture;
                    repository.Save(item);
                    repository.Flush();
                    return item.ID;
                }
            }
            catch (Exception)
            {
                return 0;
            }


        }
    }
}
